from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .webstest import ssh
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
import random
from django.http import JsonResponse

from tools.fass import webtool
# Create your views here.



class GetCpuView(APIView):
    def get(self,request):
        data = ssh.get_cpu_info()
        return Response(data)





class TestWebsocket(APIView):
    def post(self, request):
        # 获取信息
        # 查询数据库对比
        input = request.data['input']
        channel_layer = get_channel_layer()
        room = request.data['user_id']
        res = webtool(input)
        print(res)
        async_to_sync(channel_layer.group_send)(
            room + '1001',  # 房间组名
            {
                'type': 'send_to_chrome',  # 消费者中处理的函数
                'data': res
            }
        )
        return JsonResponse({"code": 200, "msg": "更新数据成功"})